// pages/Personal/myorder/myorder.js
var ikz = require('../../Common/tools.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,//页数
    limit: 6,//每页条数
    order_info: '',//订单信息
    prodis: 'none',//底部提示是否显示
  },
  //跳转到详情页
  toContents: function (e) {
    var oid = e.currentTarget.dataset.oid;
    var data = {
      oid: oid,
    }
    ikz.nav('../orcon/orcon', 'n', data);
  },
  //重新支付
  repay: function (e) {
    var oid = e.currentTarget.dataset.oid;
    var path = 'Perorder/rePay';
    var data = {
      oid: oid,
    };
    wx.showModal({
      title: '提示',
      content: '您确认支付吗？',
      success: function (res) {
        if (res.confirm) {
          var that = this;
          ikz.request(path, data, true, (res) => {
            console.log(res.data);
            console.log(233444);
            if (res.data.status == 'success') {
              console.log(res.data)
              var payment = res.data.pay;
              var outnum = res.data.out_num;
              ikz.pay(payment, (res) => {
                //提示 并跳转
                wx.showModal({
                  title: '提示',
                  content: '查看支付结果',
                  success: function (res) {
                    console.log(res);
                    if (res.confirm) {
                      var path = 'Release/checkHeadOr';
                      var data = {
                        num: outnum
                      }
                      ikz.request(path, data, true, (res) => {
                        if (res.data == 'success') {
                          wx.showModal({
                            title: '提示',
                            content: '支付成功！',
                            showCancel: false,
                            success: function (res) {
                              wx.navigateTo({
                                url: '../myorder/myorder'
                              })
                            }
                          });
                        } else {
                          wx.showModal({
                            title: '提示',
                            content: '支付失败，请重试',
                            showCancel: false
                          });
                        }
                      });
                    }

                  }
                })
              })
            } else if (res.data.status == 'notop') {
              wx.showModal({
                title: '提示',
                content: '发布失败，头条置顶数已满！',
                showCancel: false
              })
            } else {
              wx.showModal({
                title: '加载失败',
                content: '请稍后重试！',
                showCancel: false
              })
            }
          });
        } else if (res.cancel) {
          return false;
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    ikz.over(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var page = 1;
    var limit = that.data.limit;
    var path = 'Perorder/myOrder';
    var data = {
      page: page,
      limit: limit,
    }
    ikz.request(path, data, true, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        console.log(res.data.order_info)
        that.setData({
          order_info: res.data.order_info,
          page: page,
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
    var page = that.data.page + 1;
    var limit = that.data.limit;
    var old_info = that.data.order_info;
    var path = 'Perorder/myOrder';
    var data = {
      page: page,
      limit: limit,
    }

    ikz.request(path, data, true, (res) => {
      console.log(res);
      console.log(6767);
      if (res.data.status == 'success') {
        var new_info = res.data.order_info;
        if (new_info.length) {
          var new_info = old_info.concat(new_info);
          var prodis = 'none';
        } else {
          var new_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          order_info: new_info,
          page: page,
          prodis: prodis,
        })
      } else {
        wx.showModal({
          title: '加载失败',
          content: '请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})